package inet.app.tuvi.util;

import inet.app.tuvi.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class UniversalImageUtil {
	
	private static UniversalImageUtil instance = null;
	private ImageLoader mImageLoader;
	private DisplayImageOptions mOptions;
//	private ImageLoadingListener mImageLoadingListener;
//	private Animation mAnimForLoading;
	
	public UniversalImageUtil(Context context) {
		initImageLoader();
	}
	
	public static UniversalImageUtil getInstance(Context context) {
		if(instance == null) {
			instance = new UniversalImageUtil(context);
		}
		return instance;
	}
	
	/**
	 * Init image loader in application class
	 * @param context
	 */
	public void initImageLoaderInMyApp(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}
	
	/**
	 * Init universal image loader
	 */
	private void initImageLoader() {
		mImageLoader = ImageLoader.getInstance();
		mOptions = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
//		mAnimForLoading = AnimationUtils.loadAnimation(mContext, R.anim.alpha);
//		mImageLoadingListener = new ImageLoadingListener() {
//			
//			@Override
//			public void onLoadingStarted(String arg0, View arg1) {}
//			
//			@Override
//			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {}
//			
//			@Override
//			public void onLoadingComplete(String str, View view, Bitmap bmp) {
//				view.setAnimation(mAnimForLoading);
//				mAnimForLoading.start();
//			}
//			
//			@Override
//			public void onLoadingCancelled(String arg0, View arg1) {}
//		};
	}
	
	public void displayImage(String imgUri, ImageView imgView, ImageLoadingListener imgListener) {
		mImageLoader.displayImage(imgUri, imgView, mOptions, imgListener);
	}
	
	public void displayImage(String imgUri, ImageView imgView) {
		mImageLoader.displayImage(imgUri, imgView, mOptions, null);
	}
	
	public ImageLoader getImageLoader() {
		return mImageLoader;
	}
	
	public DisplayImageOptions getImageOptions() {
		return mOptions;
	}

}
